package com.example.spring3.controller;

//import com.rgzn.charpter5.entity.Blog;
//import com.rgzn.charpter5.service.BlogService;
import com.example.spring3.entity.Blog;
import com.example.spring3.service.BlogService;
import lombok.extern.slf4j.Slf4j;
//import org.example.finaspringboot.entity.Blog;
//import org.example.finaspringboot.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@CrossOrigin(origins = "http://localhost:5173")
@RestController
public class BlogController {
    @Autowired
    private BlogService blogService;


    @GetMapping("/api/blogs")
    public ResponseEntity getBlogsByPage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5") int size) {
        //获取当前页面的数据
        List<Blog> blogs = blogService.findByPage( page,size);
        //获取总博客数量
        int total = blogService.count();
        //构建相应数据
        Map<String,Object> map = new HashMap<>();
        map.put("blogs",blogs);
        map.put("total",total);
        return ResponseEntity.ok(map);
    }
}
